package de.tum.in.eist.poll.server;

import java.util.LinkedList;
import java.util.List;
import de.tum.in.eist.poll.pub.shared.Feedback;
import de.tum.in.eist.poll.pub.shared.Lecture;
import de.tum.in.eist.poll.pub.shared.Lecturer;
import de.tum.in.eist.poll.pub.shared.Student;
import de.tum.in.eist.poll.pub.shared.User;

public class Database1 {
	// list of all students:
	public static List<Student> students;
	// list of all lecturers:
	public static List<Lecturer> lecturers;
	public static List<Lecture> lectures;

	public static List<Feedback> feedbacks = new LinkedList<Feedback>();

	// returns a list containing all feedbacks:
	public static List<Feedback> getFeedback() {

		return feedbacks;
	}

	// uploads a new feedback:
	public static void collectFeedback(Feedback feedback) {
		// add this feedback to the list of feedbacks
		feedbacks.add(feedback);
		// add this student to list of this lecture to avoid several feedbacks
		// of one single person for the same lecture
		feedback.getLecture().addStudent(feedback.getStudent());
	}

	// returns a list containing all feedbacks for the given lectures:
	public static List<Feedback> getFeedbackForLecture(Lecture l) {
		List<Feedback> list = new LinkedList<Feedback>();
		// check all feedbacks if they concern the given lecture:
		for (Feedback feedback : feedbacks) {
			if (feedback.getLecture().equals(l)) {
				// add the feedbacks concerning this lecture to the list:
				list.add(feedback);
			}
		}
		return list;
	}


	// returns a list with all lectures:
	public static List<Lecture> getLectures() {
		if (lectures == null) { // lectures are not in the list
			lectures = new LinkedList<Lecture>();
			Lecture lecture0 = new Lecture("L0", "Organization & Introduction",
					getLecturers().get(0));
			Lecture lecture1 = new Lecture("L1", "Chapter 1", getLecturers()
					.get(1));
			// add the lectures:
			lectures.add(lecture0);
			lectures.add(lecture1);
		}
		return lectures;
	}

	// returns a list containing all students:
	public static List<Student> getStudents() {
		if (students == null) { // students are not in the list yet
			Student s1 = new Student("Student_1", "new");
			Student s2 = new Student("Student_2", "new");
			students = new LinkedList<Student>();
			// add the students:
			students.add(s1);
			students.add(s2);
		}
		return students;
	}

	/*
	 * Searches for a lecture with the given id and returns it. If there is no /
	 * lecture with this id, null will be returned.
	 */
	public Lecture getLecture(String id) {
		for (Lecture lecture : getLectures()) {
			String lectureId = lecture.getLectureID();
			if (lectureId.equals(id)) {
				return lecture;
			}
		}
		return null;
	}

	// returns a list containing all lecturers:
	public static List<Lecturer> getLecturers() {
		if (lecturers == null) {// students are not in the list yet
			Lecturer l1 = new Lecturer("Prof", "new");
			Lecturer l2 = new Lecturer("Maalej", "new");
			lecturers = new LinkedList<Lecturer>();
			// add the lecturers
			lecturers.add(l1);
			lecturers.add(l2);
		}
		return lecturers;
	}

	/*
	 * This method checks if the User is registered. If true, the user will be
	 * returned. If false, null will be returned.
	 */
	public static User getUser(String name) {
		// check if the user is a student:
		for (Student student : getStudents()) {
			String studentName = student.getName();
			if (studentName.equals(name)) {
				return student;
			}
		}
		// check if the user is a lecturer:
		for (Lecturer lecturer : getLecturers()) {
			String lecturerName = lecturer.getName();
			if (lecturerName.equals(name)) {
				return lecturer;
			}
		}
		return null;
	}
}